//    Copyright (C) Clear Office.  All rights reserved.
using System;
using System.Collections.Generic;
using Office.Core.Serialization;

namespace Office.Spreadsheet.PivotTable
{
    internal class PivotArea : IOfficeXmlSerializable
    {
        private const string xmlName = "pivotArea";
        private Axis? _axis;
        private Boolean _cacheIndex;
        private Boolean _collapsedLevelsAreSubtotals;
        private Boolean _dataOnly = true;
        private ExtensionList _extLst;
        private Int32? _field;
        private UInt32? _fieldPosition;
        private Boolean _grandCol;
        private Boolean _grandRow;
        private Boolean _labelOnly;
        private string _offset; //\\ to change...
        private Boolean _outline = true;
        private List<PivotAreaReference> _references = new List<PivotAreaReference>();
        private PivotAreaType _type;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                _field = c.GetOptionalInteger("field");
                _type = c.GetOptionalEnum("type", PivotAreaType.Normal);
                _dataOnly = c.GetOptionalBoolean("dataOnly", true);
                _labelOnly = c.GetOptionalBoolean("labelOnly", false);
                _grandRow = c.GetOptionalBoolean("grandRow", false);
                _grandCol = c.GetOptionalBoolean("grandCol", false);
                _cacheIndex = c.GetOptionalBoolean("cacheIndex", false);
                _outline = c.GetOptionalBoolean("outline", true);
                _offset = c.GetOptionalString("offset");
                _collapsedLevelsAreSubtotals = c.GetOptionalBoolean("collapsedLevelsAreSubtotals", false);
                _axis = c.GetOptionalEnum<Axis>("axis");
                _fieldPosition = c.GetOptionalUInteger("fieldPosition");
                _references.Clear();
                _references.AddRange(c.ReadOptionalListElement<PivotAreaReference>("references", "reference"));
                _extLst = c.ReadOptionalElement<ExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetOptionalInteger("field", _field);
                c.SetOptionalEnum("type", _type, PivotAreaType.Normal);
                c.SetOptionalBoolean("dataOnly", _dataOnly, true);
                c.SetOptionalBoolean("labelOnly", _labelOnly, false);
                c.SetOptionalBoolean("grandRow", _grandRow, false);
                c.SetOptionalBoolean("grandCol", _grandCol, false);
                c.SetOptionalBoolean("cacheIndex", _cacheIndex, false);
                c.SetOptionalBoolean("outline", _outline, true);
                c.SetOptionalString("offset", _offset);
                c.SetOptionalBoolean("collapsedLevelsAreSubtotals", _collapsedLevelsAreSubtotals, false);
                c.SetOptionalEnum("axis", _axis);
                c.SetOptionalUInteger("fieldPosition", _fieldPosition);
                if (_references.Count > 0)
                    using (WriteContext c2 = c.Write("references")
                        )
                    {
                        c2.WriteElements(_references);
                    }
                c.WriteOptionalElement(_extLst);
            }
        }

        #endregion
    }
}